<?php

/**
 * Class describing form input  
 */
class FormInput {

    /**
     * input name
     * @var string
     */
    public $name;

    /**
     * input type, e.g. text
     * @var string
     */
    public $inputType;

    /**
     * data type, that will be stored in this input
     * @var string
     */
    public $dataType;

    /**
     * decides whether input value will be marked as required, or not
     * @var string
     */
    public $required;

    /**
     * label displayed with label
     * @var string
     */
    public $label;

    /**
     * input optional data, e.g. class or style
     * @var Array
     */
    public $optionalData;

    /**
     * decides whether new line should be automatically printed after this input
     * @var string
     */
    public $newLine;

    /**
     * html container that contains this input, for example <div></div>
     * @var string 
     */
    public $container;

    /**
     * Default constructor
     * @param string $name
     * @param string $required
     * @param string $inputType
     * @param string $dataType
     * @param string $label
     * @param Array $optionalData
     * @param string $newLine
     * @param string $container 
     */
    public function __construct($name, $required, $inputType = null, $dataType = null, $label = null, $optionalData = null, $newLine = false, $container = '') {
        $this->name = $name;
        $this->required = ($required) ? 'required' : '';
        $this->inputType = isset($inputType) ? $inputType : "text";
        $this->dataType = isset($dataType) ? $dataType : "string";
        $this->label = isset($label) ? $label : $name;
        $this->optionalData = isset($optionalData) ? $optionalData : array();
        if ($newLine == true)
            $this->newLine = '<br/>';
        else
            $this->newLine = '';
        $this->container = $container;
    }

    /**
     * Renders form input according to its type
     * @return String 
     */
    public function renderFormInput() {
        $html = '';
        if ($this->label != '')
            $html = "<label for='{$this->name}'>{$this->label}</label>\n";
        if ($this->inputType == 'textarea') {
            $html .= "<textarea name='%s' %s ";
            foreach ($this->optionalData as $key => $value)
                $html .= "$key = '$value'";
            $html .= "></textarea>{$this->newLine}";
            $html = sprintf($html, $this->name, $this->required);
        } else if ($this->inputType == 'button') {
            $html = '<button type="button" id="%s" ';
            foreach ($this->optionalData as $key => $value)
                $html .= "$key = '$value'";
            $html .= '>%s</button>%s';
            $html = sprintf($html, $this->name, $this->label, $this->newLine);
        } else {
            $html .= "<input name='%s' type='%s' ";
            foreach ($this->optionalData as $key => $value)
                $html .= "$key=\"$value\" ";
            $html .= "%s />{$this->newLine}";
            $html = sprintf($html, $this->name, $this->inputType, $this->required);
        }
        if ($this->container != '')
            $html = sprintf($this->container, $html);
        return $html;
    }

    /**
     * Validates form input data
     * @param Array $data 
     * @return Array
     */
    public function validateData($data) {
        $errors = array();
        if (!isset($data) && $this->required)
            $errors[] = "Pole {$this->name} jest wymagane, lecz jego zawartość nie została podana.";
        if (!call_user_func("is_{$this->dataType}", $data))
            $errors[] = "Pole {$this->name} zawiera niewłaściwe dane.";
        return $errors;
    }

}
?>

